package io.renren.modules.police.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.modules.police.dao.PoliceServiceAppointmentDao;
import io.renren.modules.police.dto.PoliceServiceAppointmentDTO;
import io.renren.modules.police.dto.PoliceServiceTypeDTO;
import io.renren.modules.police.entity.PoliceServiceAppointmentEntity;
import io.renren.modules.police.service.PoliceServiceAppointmentService;
import io.renren.modules.police.service.PoliceServiceTypeService;
import io.renren.modules.security.user.SecurityUser;
import io.renren.modules.sys.dto.SysUserDTO;
import io.renren.modules.sys.service.SysUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 服务预约表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2023-09-02
 */
@Service
public class PoliceServiceAppointmentServiceImpl extends CrudServiceImpl<PoliceServiceAppointmentDao, PoliceServiceAppointmentEntity, PoliceServiceAppointmentDTO> implements PoliceServiceAppointmentService {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private PoliceServiceTypeService policeServiceTypeService;

    @Override
    public QueryWrapper<PoliceServiceAppointmentEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String keyWord = (String)params.get("keyWord");
        String serviceTypeId = (String)params.get("serviceTypeId");
        String status = (String)params.get("status");

        QueryWrapper<PoliceServiceAppointmentEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        if (StringUtils.isNotBlank(keyWord)) {
            List<SysUserDTO> userDTOList = sysUserService.getByRealName(keyWord);
            if (CollectionUtils.isNotEmpty(userDTOList)) {
                wrapper.in("user_id", userDTOList.stream().map(e -> e.getId()).collect(Collectors.toList()));
            }
        }
        if (StringUtils.isNotBlank(serviceTypeId)) {
            wrapper.eq("service_type_id", Long.valueOf(serviceTypeId));
        }
        if (StringUtils.isNotBlank(status)) {
            wrapper.eq("status", Integer.valueOf(status));
        }
        return wrapper;
    }


    @Override
    public PageData<PoliceServiceAppointmentDTO> getPage(Map<String, Object> params) {
        PageData<PoliceServiceAppointmentDTO> page = this.page(params);
        page.getList().forEach(e -> {
            PoliceServiceTypeDTO policeServiceTypeDTO = policeServiceTypeService.get(e.getServiceTypeId());
            e.setServiceTypeName(policeServiceTypeDTO.getTypeName());
            SysUserDTO creatorDTO = sysUserService.get(e.getCreator());
            e.setCreatorName(creatorDTO.getRealName());
            e.setUserName(creatorDTO.getRealName());
            SysUserDTO updaterDTO = sysUserService.get(e.getUpdater());
            e.setUpdaterName(updaterDTO.getRealName());
        });
        return page;
    }

    @Override
    public void saveAppointment(PoliceServiceAppointmentDTO dto) {
        Long userId = SecurityUser.getUserId();
        dto.setUserId(userId);
        dto.setStatus(0);
        this.save(dto);
    }

    @Override
    public List<PoliceServiceAppointmentDTO> getServiceAppointmentByStatus(Integer status) {
        Long userId = SecurityUser.getUserId();
        QueryWrapper<PoliceServiceAppointmentEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("status", status);
        List<PoliceServiceAppointmentEntity> entityList = this.baseDao.selectList(wrapper);
        List<PoliceServiceAppointmentDTO> dtoList = ConvertUtils.sourceToTarget(entityList, PoliceServiceAppointmentDTO.class);
        dtoList.forEach(dto -> {
            PoliceServiceTypeDTO policeServiceTypeDTO = policeServiceTypeService.get(dto.getServiceTypeId());
            dto.setServiceTypeName(policeServiceTypeDTO.getTypeName());
            SysUserDTO creatorDTO = sysUserService.get(dto.getCreator());
            dto.setCreatorName(creatorDTO.getRealName());
            dto.setUserName(creatorDTO.getRealName());
            SysUserDTO updaterDTO = sysUserService.get(dto.getUpdater());
            dto.setUpdaterName(updaterDTO.getRealName());
        });
        return dtoList;
    }
}